package com.loong.common.rabbitmq.tools;

import com.loong.global.constant.MQConstant;
import com.loong.global.enums.mq.QueueEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Slf4j
@Component
public class RabbitmqTools {


    @Resource(name = "defaultRabbitAdmin")
    private RabbitAdmin defaultRabbitAdmin;

    public void initQueue(QueueEnums enums) {
        log.debug("开始初始化mq:{}",enums.getQueueName());
        DirectExchange exchange = new DirectExchange(MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE, true, false);
        defaultRabbitAdmin.declareExchange(exchange);
        initProgressQueue(enums, exchange);
        log.debug("初始化完成:{}",enums.getQueueName());
    }
    public void initTopicQueue(QueueEnums enums) {
        log.debug("开始初始化TopicMq:{}",enums.getQueueName());
        TopicExchange exchange = new TopicExchange(MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE, true, false);
        defaultRabbitAdmin.declareExchange(exchange);
        initProgressQueue(enums, exchange);
        log.debug("初始化完成TopicMq:{}",enums.getQueueName());
    }

    public void initDelayTopicQueue(QueueEnums enums){
        // init exchange
        TopicExchange exchange = new TopicExchange(MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE, true, false);
        defaultRabbitAdmin.declareExchange(exchange);
       // init delay topic queue
        Queue delayQueue = new Queue(MQConstant.BASE_PREFIX+enums.getQueueName()+MQConstant.BASE_SUFFIX.TOPIC_DELAY_QUEUE, true, false, false);
        delayQueue.addArgument("x-dead-letter-exchange", MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE);
        delayQueue.addArgument("x-dead-letter-routing-key", MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.DEAD_ROUTE_KET);
        defaultRabbitAdmin.declareQueue(delayQueue);
        Binding delayQueueBinding = BindingBuilder.bind(delayQueue).to(exchange).with(MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.TOPIC_ROUTE_KET);
        defaultRabbitAdmin.declareBinding(delayQueueBinding);
        // init process queue
        initDeadQueue(enums, exchange);
    }
    public void initDelayQueue(QueueEnums enums){
        // init exchange
        DirectExchange exchange = new DirectExchange(MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE, true, false);
        defaultRabbitAdmin.declareExchange(exchange);
        // init delay queue
        Queue delayQueue = new Queue(MQConstant.BASE_PREFIX+enums.getQueueName()+MQConstant.BASE_SUFFIX.DELAY_QUEUE, true, false, false);
        delayQueue.addArgument("x-dead-letter-exchange", MQConstant.BASE_PREFIX+enums.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE);
        delayQueue.addArgument("x-dead-letter-routing-key", MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.ROUTE_KEY);
        defaultRabbitAdmin.declareQueue(delayQueue);
        Binding delayQueueBinding = BindingBuilder.bind(delayQueue).to(exchange).with(MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.DELAY_ROUTE_KET);
        defaultRabbitAdmin.declareBinding(delayQueueBinding);
        // init process queue
        initProgressQueue(enums, exchange);
    }

    private void initProgressQueue(QueueEnums enums, DirectExchange exchange) {
        Queue progressQueue = new Queue(MQConstant.BASE_PREFIX+enums.getQueueName()+MQConstant.BASE_SUFFIX.QUEUE, true, false, false);
        defaultRabbitAdmin.declareQueue(progressQueue);
        Binding progressQueueBinding = BindingBuilder.bind(progressQueue).to(exchange).with(MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.ROUTE_KEY);
        defaultRabbitAdmin.declareBinding(progressQueueBinding);
    }
    private void initProgressQueue(QueueEnums enums, TopicExchange exchange) {
        Queue progressQueue = new Queue(MQConstant.BASE_PREFIX+enums.getQueueName()+MQConstant.BASE_SUFFIX.QUEUE, true, false, false);
        defaultRabbitAdmin.declareQueue(progressQueue);
        Binding progressQueueBinding = BindingBuilder.bind(progressQueue).to(exchange).with(MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.TOPIC_ROUTE_KET);
        defaultRabbitAdmin.declareBinding(progressQueueBinding);
    }
    private void initDeadQueue(QueueEnums enums, TopicExchange exchange) {
        Queue progressQueue = new Queue(MQConstant.BASE_PREFIX+enums.getQueueName()+MQConstant.BASE_SUFFIX.DEAD_QUEUE, true, false, false);
        defaultRabbitAdmin.declareQueue(progressQueue);
        Binding progressQueueBinding = BindingBuilder.bind(progressQueue).to(exchange).with(MQConstant.BASE_PREFIX+enums.getRouterKey()+MQConstant.BASE_SUFFIX.DEAD_TOPIC_ROUTE_KET);
        defaultRabbitAdmin.declareBinding(progressQueueBinding);
    }

}
